package com.jy.admin.rest;

import com.fqgj.common.api.Response;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.jy.admin.rest.request.ProductAddRequest;
import com.jy.admin.rest.response.ProductSearchRequest;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.jy.admin.rest.request.ProductUpdateRequest;
import com.jy.admin.rest.response.ProductListResponse;
import com.jy.bo.common.conf.api.ApiResponse;
import com.robert.vesta.util.IdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/11/10
 * Time: 11:14 上午
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductFacade productFacade;

    @RequestMapping(value = "/add")
    public ApiResponse<ProductListResponse> add(@RequestBody ProductAddRequest request) {

        Response<ProductCategory> categoryByCode = productFacade.getCategoryByCode(request.getProductCategoryCode());
        if (categoryByCode != null && categoryByCode.getData() != null) {
            return new ApiResponse(601, "category exist already");
        }

        ProductCategory productCategory = new ProductCategory()
                .setName(request.getName())
                .setLogo(request.getLogo())
                .setMerchantCode(IdUtils.genId() + "")
                .setCode(request.getProductCategoryCode())

                .setCategoryDesc(request.getCategoryDesc())
                .setIsSelling(0)
                .setApplyRate(28)
                .setMoneyFloor(request.getMoneyFloor().toString())
                .setMoneyUpper(request.getMoneyUpper().toString())

                .setCategoryType(3)
                .setDailyApplyLimit(1000)
                .setMerchantDoaminUrl(request.getMerchantDomainUrl())
                .setMerchantPublicKey(IdUtils.genId() + "")

                .setForGp(request.getForGp());

        productCategory.setSort(request.getSort());

        String extraInfo = "";
        Map<String, Object> temp = new HashMap<>();
        if (request.getJumpType() != null) {
            temp.put("jumpType", request.getJumpType());
        }
        if (request.getJumpUrl() != null) {
            temp.put("jumpUrl", request.getJumpUrl());
        }

        extraInfo = JsonStream.serialize(temp);
        productCategory.setExtraInfo(extraInfo);

        productFacade.addProductCategory(productCategory);

        return new ApiResponse();

    }

    @RequestMapping(value = "/list")
    public ApiResponse<ProductListResponse> list(@RequestBody ProductSearchRequest request) {
        Response<List<ProductCategory>> allProductCategory = productFacade.getAllProductCategory();
        return new ApiResponse<ProductListResponse>(new ProductListResponse(allProductCategory.getData(), request.getForGp()));
    }

    @RequestMapping(value = "/update")
    public ApiResponse update(@RequestBody ProductUpdateRequest request) {
        Response<ProductCategory> categoryByCode = productFacade.getCategoryByCode(request.getCode());
        if (categoryByCode == null || categoryByCode.getData() == null) {
            return new ApiResponse(601, "category not exist");
        }

        ProductCategory productCategory = new ProductCategory()
                .setCode(request.getCode())
                .setIsSelling(request.getSelling())
                .setApplyRate(request.getApplyPercent())
                .setDailyApplyLimit(request.getDayLimit())
                .setCategoryType(request.getPosition())
                .setForGp(request.getForGp());

        productCategory.setSort(request.getDisplayNo());

        String extraInfo = categoryByCode.getData().getExtraInfo();
        Map<String, Object> temp = new HashMap<>();
        if (StringUtils.isNotEmpty(extraInfo)) {
            Map<String, Any> stringAnyMap = JsonIterator.deserialize(extraInfo).asMap();
            for (String key : stringAnyMap.keySet()) {
                temp.put(key, stringAnyMap.get(key).toString());
            }
        }
        if (request.getLendingUserType() != null) {
            temp.put("lendingUserType", request.getLendingUserType());
        }
        if (request.getPromotionTagType() != null) {
            temp.put("promotionTagType", request.getPromotionTagType());
        }
        if (request.getFullType() != null) {
            temp.put("fullType", request.getFullType());
        }
        if (request.getJumpType() != null) {
            temp.put("jumpType", request.getJumpType());
        }
        if (request.getJumpUrl() != null) {
            temp.put("jumpUrl", request.getJumpUrl());
        }

        extraInfo = JsonStream.serialize(temp);
        productCategory.setExtraInfo(extraInfo);
        productFacade.updateProductCategoryByCode(productCategory);

        return new ApiResponse();
    }


}
